package top.mschen.blog.controller;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.thymeleaf.TemplateEngine;
import top.mschen.blog.common.utils.MailUtils;
import top.mschen.blog.dto.MailBean;

/**
 * 启动后执行可以用来预加载
 */
@Component
public class AppRunner  implements CommandLineRunner {

//    private AsyncTestImpl asyncTestImpl;

//    public AppRunner(AsyncTestImpl asyncTestImpl) {
//        this.asyncTestImpl = asyncTestImpl;
//    }

    private MailUtils mailUtils;

    private TemplateEngine templateEngine;

    private FreeMarkerConfigurer configurer;

    public AppRunner(MailUtils mailUtils, TemplateEngine templateEngine, FreeMarkerConfigurer freeMarkerConfigurer) {
        this.mailUtils = mailUtils;
        this.templateEngine = templateEngine;
        this.configurer = freeMarkerConfigurer;
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("------------- SpringBoot集成JavaMail实现邮件发送开始 -------------");
        MailBean mailBean = MailBean.getMailBean();

//        System.out.println(asyncTestImpl);
//        asyncTestImpl.AsynInvokeSimple();
//        asyncTestImpl.AsynInvokeParam("hello word");
//        asyncTestImpl.AsynInvokeReturn(1);

/*        *//**
         * 发送html
         *//*
      String html =
        "<html>"
                + "<body>"
                + "<p><h1>SpringBoot集成JavaMail实现正文为HTML的邮件发送功能</h1></p>"
                + "</body>"
                + "</html>";
        mailBean.setSubject("SpringBoot集成JavaMail实现邮件发送");
        mailBean.setText(html);
        mailUtils.sendMailHtml(mailBean);*/

        /**
         * 附件邮件发送测试
         */
//      String filePath="D:\\image1.jpg";
//      FileSystemResource file = new FileSystemResource(new File(filePath));
//      String attachmentFilename = filePath.substring(filePath.lastIndexOf(File.separator));
//      mailBean.setSubject("SpringBoot集成JavaMail实现邮件发送");
//      mailBean.setText("SpringBoot集成JavaMail实现附件邮件发送");
//      mailBean.setFile(file);
//      mailBean.setAttachmentFilename(attachmentFilename);
//      mailUtils.sendMailAttachment(mailBean);



        /**
         * 内联资源邮件发送测试
         */
//          String filePath="D:\\image1.jpg";
//          String contentId = UUID.randomUUID().toString().replace("-", "");
//          /* //       * <img src=\'cid:" + contentId + "\' >
//           * cdi:是固定的，其后连接内联资源的的ID（保证唯一即可） //       */
//          String content = "<html><body>内联资源邮件发送：<img src=\'cid:" + contentId + "\' ></body></html>";
//          FileSystemResource file = new FileSystemResource(new File(filePath));
//          mailBean.setSubject("SpringBoot集成JavaMail实现邮件发送");
//          mailBean.setText(content);
//          mailBean.setFile(file);
//          mailBean.setContentId(contentId);
//          mailUtils.sendMailInline(mailBean);


        /**
         * 使用模板  thymeleaf 发送邮件
         */
//        Context context = new Context();
//        context.setVariable("user", "Tyrone");
//        String content = templateEngine.process("emailTemplate", context);
//        mailBean.setSubject("SpringBoot集成JavaMail实现邮件发送");
//        mailBean.setText(content);
//        mailUtils.sendMailTemplate(mailBean);


//        for (int i = 0; i < 10; i++) {
            /**
             * 使用freemarker
             */
//            Map<String, Object> model = new HashMap<>();
//            Map<String,Object> message = new HashMap<String,Object>();
//            message.put("messageCode","MissingParameter");
//            message.put("messageStatus","Failed");
//            message.put("cause","缺少参数,请确认");
//            model.put("params", message);
//            Template template = configurer.getConfiguration().getTemplate("mail.ftl");
//            String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
//            mailBean.setText(text);
//            mailBean.setSubject("使用freemarker");
//            mailUtils.sendMailTemplate(mailBean);
//        }
    }
}